package com.yangtzeu.jwc.utils;

import java.util.LinkedList;
import java.util.List;

import android.app.Activity;

/**
 * 控制全局退出(退出应用)
 *
 * @author ZhangDI
 */
public class AllActivitys {
    private static AllActivitys apps;
    private List<Activity> appList;

    private AllActivitys() {
        appList = new LinkedList<Activity>();
    }

    public static AllActivitys getInstance() {
        if (apps == null) {
            apps = new AllActivitys();
        }
        return apps;
    }

    public void addActivity(Activity activity) {
        if (!appList.contains(activity)) {
            appList.add(activity);
        }
    }

    public void removeActivity(Activity activity) {
        appList.remove(activity);
    }

    public void onCreate(Activity activity) {
        addActivity(activity);
    }

    public void onResume(Activity activity) {

    }

    public void onPause(Activity activity) {

    }

    public void onDestroy(Activity activity) {
        removeActivity(activity);
    }

    /**
     * 退出系统
     */
    public void exit() {
        try {
            for (Activity activity : appList) {
                if (activity != null)
                    activity.finish();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.exit(0);
        }
    }
}
